#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> 
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>

#define S_PORT 8080

int main(){

	int cfd,ret;
	int cont = 10;
	char buf[BUFSIZ];
	struct sockaddr_in s_addr;
	//1. socket
	cfd = socket(AF_INET,SOCK_STREAM,0);
	s_addr.sin_family = AF_INET;
	s_addr.sin_port = htons(S_PORT);
	inet_pton(AF_INET,"127.0.0.1",&s_addr.sin_addr);
	//2. connect
	int con = connect(cfd,(struct sockaddr *)&s_addr,sizeof(s_addr));
	if(con == -1){
		perror("connect");
		exit(1);
	}

	while(--cont){
		//3. write
		write(cfd,"hello\n",6);
		//4. read
		ret = read(cfd,buf,sizeof(buf));
		write(STDOUT_FILENO,buf,ret);
		sleep(1);
	}
		//5. close
		close(cfd);
}
